<template>
  <div id="app">
    <h2>测试题</h2>
    <SubjectItem
      :num="num.length < 5 ? getNum() : num"
      @compute="compute"
    ></SubjectItem>
    <div>
      <FlagItem :answers="answers"></FlagItem>
    </div>
  </div>
</template>

<script>
import FlagItem from "./components/FlagItem.vue";
import SubjectItem from "./components/SubjectItem.vue";
export default {
  name: "App",
  components: {
    FlagItem,
    SubjectItem,
  },
  data() {
    return {
      num: [],
      answers: [],
      index: "",
    };
  },
  methods: {
    getNum() {
      this.num.push({
        one: Math.floor(Math.random() * 10),
        two: Math.floor(Math.random() * 10),
      });
    },
    compute(answer, index) {
      this.index = index;
      this.num[index].one + this.num[index].two === answer
        ? (this.answers[index] = {
            name: "回答正确",
            id: +new Date(),
            index: index,
          })
        : answer === ""
        ? (this.answers[index] = {
            name: "未完成",
            id: +new Date(),
            index: index,
          })
        : (this.answers[index] = {
            name: "回答错误",
            id: +new Date(),
            index: index,
          });
    },
  },
};
</script>

<style>
body {
  background-color: #eee;
}

#app {
  background-color: #fff;
  width: 500px;
  margin: 50px auto;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  padding: 2em;
}
</style>
